package ru.jexp.rcp.supply.update;

public class P2Util
{
/*	// XXX Check for updates to this application and return true if
	// we have installed updates and need a restart.
	// This method is intentionally long and ugly in order to provide
	// "one-stop-shopping" for how to check for and perform an update.
	public static boolean checkForUpdates() {
		// Before we show a progress dialog, at least find out that we have
		// installed content and repos to check.
		final IProfileRegistry profileRegistry = (IProfileRegistry) ServiceHelper
				.getService(Activator.bundleContext, IProfileRegistry.class
						.getName());
		if (profileRegistry == null)
			return false;
		final IProfile profile = profileRegistry.getProfile(IProfileRegistry.SELF);
		if (profile == null)
			return false;

		// We are going to look for updates to all IU's in the profile. A
		// different query could be used if we are looking for updates to
		// a subset. For example, the p2 UI only looks for updates to those
		// IU's marked with a special property.
		final Collector collector = profile.query(arg0, arg1);//(QueryUtil.ANY,new Collector(), null);
		if (collector.isEmpty())
			return false;
		final IMetadataRepositoryManager manager = (IMetadataRepositoryManager) ServiceHelper
				.getService(Activator.bundleContext,
						IMetadataRepositoryManager.class.getName());
		if (manager == null)
			return false;
		final URI[] reposToSearch = manager
				.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
		if (reposToSearch.length == 0)
			return false;
		final IPlanner planner = (IPlanner) ServiceHelper.getService(Activator.bundleContext, IPlanner.class.getName());
		if (planner == null)
			return false;
		// Looking in all known repositories for updates for each IU in the profile
		final boolean[] didWeUpdate = new boolean[1];
		didWeUpdate[0] = false;
		IRunnableWithProgress runnable = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor)
					throws InvocationTargetException, InterruptedException {
				// We'll break progress up into 4 steps.
				// 1.  Load repos - it is not strictly necessary to do this.
				//     The planner will do it for us.  However, burying this
				//     in the planner's progress reporting will not 
				//     show enough progress initially, so we do it manually.
                // 2.  Get update list
				// 3.  Build a profile change request and get a provisioning plan
				// 4.  Perform the provisioning plan.
				// 1.  Load repos
				for (int i=0; i<reposToSearch.length; i++)
					try {
						if (monitor.isCanceled())
							throw new InterruptedException();
						manager.loadRepository(reposToSearch[i], null);
					} catch (ProvisionException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
				// 2.  Get update list.
				// First we look for replacement IU's for each IU
				ArrayList iusWithUpdates = new ArrayList();
				ArrayList replacementIUs = new ArrayList();
				Iterator iter = collector.iterator();
				ProvisioningContext pc = new ProvisioningContext(reposToSearch);
				while (iter.hasNext()) {
					if (monitor.isCanceled())
						throw new InterruptedException();
					IInstallableUnit iu = (IInstallableUnit) iter.next();
					IInstallableUnit[] replacements = planner.updatesFor(iu,
							pc, null);
					if (replacements.length > 0) {
						iusWithUpdates.add(iu);
						if (replacements.length == 1)
							replacementIUs.add(replacements[0]);
						else {
							IInstallableUnit repl = replacements[0];
							for (int i = 1; i < replacements.length; i++)
								if (replacements[i].getVersion().compareTo(
										repl.getVersion()) > 0)
									repl = replacements[i];
							replacementIUs.add(repl);
						}
					}
				}
				// Did we find any updates?
				if (iusWithUpdates.size() > 0) {
					if (monitor.isCanceled())
						throw new InterruptedException();
					// 3.  Build a profile change request and get a provisioning plan
					ProfileChangeRequest changeRequest = new ProfileChangeRequest(
							profile);
					changeRequest
							.removeInstallableUnits((IInstallableUnit[]) iusWithUpdates
									.toArray(new IInstallableUnit[iusWithUpdates
											.size()]));
					changeRequest
							.addInstallableUnits((IInstallableUnit[]) replacementIUs
									.toArray(new IInstallableUnit[replacementIUs
											.size()]));
					ProvisioningPlan plan = planner.getProvisioningPlan(
							changeRequest, pc, null);
					if (plan.getStatus().getSeverity() == IStatus.CANCEL)
						throw new InterruptedException();
					if (plan.getStatus().getSeverity() != IStatus.ERROR) {
						IEngine engine = (IEngine) ServiceHelper.getService(
								Activator.bundleContext, IEngine.class
										.getName());
						IArtifactRepositoryManager artifactMgr = (IArtifactRepositoryManager) ServiceHelper
								.getService(Activator.bundleContext,
										IArtifactRepositoryManager.class
												.getName());
						if (engine != null && artifactMgr != null) {
							// 4.  Perform the provisioning plan
							pc
									.setArtifactRepositories(artifactMgr
											.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL));
							IStatus status = engine.perform(profile,
									new DefaultPhaseSet(), plan.getOperands(),
									pc, null);
							if (status.getSeverity() == IStatus.CANCEL)
								throw new InterruptedException();
							if (status.getSeverity() != IStatus.ERROR) {
								didWeUpdate[0] = true;
							}
						}
					}
				}
			}
		};
		try {
			new ProgressMonitorDialog(null).run(true, true, runnable);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			return false;
		} catch (InterruptedException e) {
			return false;
		}
		return didWeUpdate[0];
	}*/
}
